/*****************************************************************************
 * plum_fsm_event.c
 *
 * Copyright (C) 2020 Jeasonvor 1101627719@qq.com
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 ****************************************************************************/

/*****************************************************************************
 * Included Files
 ****************************************************************************/
#include "plum_api.h"
/*****************************************************************************
 * Trace Definitions
 ****************************************************************************/
#define LOG_RUN_LEVEL LOG_LEVEL_DEBUG
#define LOG_MODULE    "fsm.event"
/*****************************************************************************
 * Pre-processor Definitions
 ****************************************************************************/
#define FSM_EVENT_MALLOC(size) malloc(size)
#define FSM_EVENT_FREE(ptr)    free(ptr)
/*****************************************************************************
 * Private Types
 ****************************************************************************/

typedef struct {
    evt_notice_t notice;
    plum_u32 *   tb;
    plum_u32     cnt;

    struct list_head list;
} fsm_evt_t;
/*****************************************************************************
 * Private Function Prototypes
 ****************************************************************************/

/*****************************************************************************
 * Private Data
 ****************************************************************************/
PLUM_PRIVATE
fsm_evt_t evt_head;
/*****************************************************************************
 * Public Data
 ****************************************************************************/

/*****************************************************************************
 * Private Functions
 ****************************************************************************/

/*****************************************************************************
 * Public Functions
 ****************************************************************************/

PLUM_PUBLIC
plum_void plum_cal_fsm_evt_publish(plum_u32 evt_id)
{
    plum_u32          loop;
    struct list_head *pos;
    list_for_each(pos, &evt_head.list)
    {
        fsm_evt_t *node = list_entry(pos, fsm_evt_t, list);
        for (loop = 0; loop < node->cnt; loop++) {
            if (node->tb[loop] == evt_id) {
                node->notice(evt_id);
                break;
            }
        }
    }
}

PLUM_PUBLIC
plum_s32 plum_cal_fsm_evt_subcribe(evt_notice_t cb, plum_u32 *evt_tb,
                                   plum_u32 cnt)
{
    plum_s32 rc = PLUM_ECODE_OK;

    do {
        fsm_evt_t *node = FSM_EVENT_MALLOC(sizeof(fsm_evt_t));
        if (node) {
            memset(node, 0, sizeof(fsm_evt_t));

            node->notice = cb;
            node->cnt    = cnt;
            node->tb     = FSM_EVENT_MALLOC(sizeof(plum_u32) * cnt);
            if (!node->tb) {
                FSM_EVENT_FREE(node);
                LOG_E("malloc memery err");
                rc = PLUM_ECODE_EMEM;
                break;
            }

            memcpy(node->tb, evt_tb, cnt);
            list_add(&node->list, &evt_head.list);
        }
        else {
            LOG_E("malloc memery err");
            rc = PLUM_ECODE_EMEM;
            break;
        }
    } while (0);

    return (rc);
}

/****************************************************************************/
/*                                                                          */
/*  End of file.                                                            */
/*                                                                          */
/****************************************************************************/
